﻿package com.vicentediaz.data
{
    import com.vicentediaz.data.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;

    public class ExternalAssets extends EventDispatcher
    {
        private var config:Config;
        private var _allLoaded:Boolean = false;
        public var mainMovieLoader:Loader;
        private var autoLoad:Boolean = false;
        private static const SOUNDS:String = "SOUNDS";
        public static var verbose:Boolean = false;
        private static const MAIN_MOVIE:String = "MAIN_MOVIE";
        private static const GALLERY:String = "GALLERY";
        private static var _autoLoad:Boolean;
        public static var instance:ExternalAssets;

        public function ExternalAssets(param1:IEventDispatcher = null)
        {
            super(param1);
            this.config = Config.getInstance();
            if (this.config.loaded)
            {
                this.onConfigLoaded(null);
            }
            else
            {
                this.config.addEventListener(ConfigEvent.CONFIG_LOADED, this.onConfigLoaded, false, 0, true);
            }// end else if
            return;
        }// end function

        private function onSoundsXMLLoaded(param1:DAOEvent) : void
        {
            var _loc_2:* = Sounds.getInstance();
            _loc_2.loadSounds(DAO.getInstance().sounds);
            _loc_2.addEventListener(SoundsLoaderEvent.NEXT_SOUND_LOADED, this.onSoundLoaded, false, 0, true);
            _loc_2.addEventListener(SoundsLoaderEvent.ALL_SOUNDS_LOADED, this.onSoundsLoaded, false, 0, true);
            return;
        }// end function

        private function onThumbsLoaded(param1:GalleryEvent) : void
        {
            dispatchEvent(new ExternalAssetsEvent(ExternalAssetsEvent.GALLERY_LOADED));
            if (this.autoLoad)
            {
                this.nextLoad(GALLERY);
            }// end if
            return;
        }// end function

        private function onThumbLoaded(param1:GalleryEvent) : void
        {
            var _loc_2:* = Gallery.getInstance();
            var _loc_3:* = new ExternalAssetsEvent(ExternalAssetsEvent.GALLERY_PROGRESS);
            _loc_3.progress = _loc_2.loadedThumbs / _loc_2.totalThumbs;
            dispatchEvent(_loc_3);
            return;
        }// end function

        private function onConfigLoaded(param1:ConfigEvent) : void
        {
            dispatchEvent(new ExternalAssetsEvent(ExternalAssetsEvent.READY));
            if (_autoLoad)
            {
                this.loadAll();
            }// end if
            return;
        }// end function

        private function onSoundLoaded(param1:SoundsLoaderEvent) : void
        {
            var _loc_2:* = Sounds.getInstance();
            var _loc_3:* = new ExternalAssetsEvent(ExternalAssetsEvent.SOUND_PROGRESS);
            _loc_3.progress = _loc_2.loadedSounds / _loc_2.totalSounds;
            dispatchEvent(_loc_3);
            return;
        }// end function

        private function onGalleryXMLLoaded(param1:DAOEvent) : void
        {
            var _loc_2:* = Gallery.getInstance();
            _loc_2.loadThumbs(DAO.getInstance().gallery);
            _loc_2.addEventListener(GalleryEvent.NEXT_THUMB_LOADED, this.onThumbLoaded, false, 0, true);
            _loc_2.addEventListener(GalleryEvent.ALL_THUMBS_LOADED, this.onThumbsLoaded, false, 0, true);
            return;
        }// end function

        private function say(param1:String) : void
        {
            if (verbose)
            {
                trace("<< EXTERNAL ASSETS : " + param1 + " >>");
            }// end if
            return;
        }// end function

        private function loadGallery() : void
        {
            this.say("cargando galer?a");
            DAO.getInstance().loadGalleryXML();
            DAO.getInstance().addEventListener(DAOEvent.GALLERY_LOADED, this.onGalleryXMLLoaded, false, 0, true);
            return;
        }// end function

        public function get allLoaded() : Boolean
        {
            return this._allLoaded;
        }// end function

        private function onSoundsLoaded(param1:SoundsLoaderEvent) : void
        {
            dispatchEvent(new ExternalAssetsEvent(ExternalAssetsEvent.SOUND_LOADED));
            if (this.autoLoad)
            {
                this.nextLoad(SOUNDS);
            }// end if
            return;
        }// end function

        private function loadSounds() : void
        {
            this.say("cargando sonidos");
            DAO.getInstance().loadSoundsXML();
            DAO.getInstance().addEventListener(DAOEvent.SOUNDS_LOADED, this.onSoundsXMLLoaded, false, 0, true);
            return;
        }// end function

        public function loadMainMovie() : void
        {
            this.say("cargando pel?cula principal");
            this.mainMovieLoader = new Loader();
            this.mainMovieLoader.load(new URLRequest("Main.swf"));
            this.mainMovieLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onMainMovieLoaded, false, 0, true);
            this.mainMovieLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.onMainMovieLoading, false, 0, true);
            return;
        }// end function

        private function nextLoad(param1:String) : void
        {
            switch(param1)
            {
                case MAIN_MOVIE:
                {
                    this.loadGallery();
                    break;
                }// end case
                case GALLERY:
                {
                    this.loadSounds();
                    break;
                }// end case
                case SOUNDS:
                {
                    this.autoLoad = false;
                    this.say("Carga completada!");
                    this._allLoaded = true;
                    dispatchEvent(new ExternalAssetsEvent(ExternalAssetsEvent.ALL_LOADED));
                    break;
                }// end case
                default:
                {
                    break;
                }// end default
            }// end switch
            return;
        }// end function

        public function loadAll() : void
        {
            this.autoLoad = true;
            this.loadMainMovie();
            return;
        }// end function

        private function onMainMovieLoading(param1:ProgressEvent) : void
        {
            var _loc_2:* = new ExternalAssetsEvent(ExternalAssetsEvent.MAIN_MOVIE_PROGRESS);
            _loc_2.progress = param1.bytesLoaded / param1.bytesTotal;
            dispatchEvent(_loc_2);
            return;
        }// end function

        private function onMainMovieLoaded(param1:Event) : void
        {
            dispatchEvent(new ExternalAssetsEvent(ExternalAssetsEvent.MAIN_MOVIE_LOADED));
            if (this.autoLoad)
            {
                this.nextLoad(MAIN_MOVIE);
            }// end if
            return;
        }// end function

        public static function getInstance(param1:Boolean = false) : ExternalAssets
        {
            _autoLoad = param1;
            instance = instance ? (instance) : (new ExternalAssets);
            return instance;
        }// end function

    }
}
